﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

public partial class Administrator_AdminControl_CategoryJob : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadAll(GeneralCategoryType.CategoryJob);
        }
    }

    private void LoadAll(long GeneralCategory_Type)
    {
        try
        {
            gridCategoryJob.DataSource = null;

            Up1VNDataContext dc = new Up1VNDataContext();
            {
                var query = from o in dc.GeneralCategories where o.GeneralCategory_Type == GeneralCategory_Type orderby o.GeneralCategory_Name select o;

                if (query.Count() >= 0)
                {
                    Session["CategoryJob"] = query;
                    gridCategoryJob.DataSource = query;
                    gridCategoryJob.DataBind();
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public int Insert(GeneralCategory gc)
    {
        int result = 1;

        try
        {
            using (Up1VNDataContext dc = new Up1VNDataContext())
            {
                var query = (from o in dc.GeneralCategories
                             where o.GeneralCategory_Type == gc.GeneralCategory_Type &&
                             o.GeneralCategory_Code == gc.GeneralCategory_Code
                             select o).ToList();

                if (query.Count == 0)
                {
                    dc.GeneralCategories.InsertOnSubmit(gc);
                    dc.SubmitChanges();
                    result = 1;
                }
                else if (query.Count > 0)
                {
                    result = -2;
                }
            }

        }
        catch (Exception ex)
        {
            result = -1;
        }

        return result;
    }

    public int Update(GeneralCategory gc)
    {
        int result = 1;

        try
        {
            using (Up1VNDataContext dc = new Up1VNDataContext())
            {
                var query = (from o in dc.GeneralCategories
                             where o.GeneralCategory_Type == gc.GeneralCategory_Type &&
                             o.GeneralCategory_Code == gc.GeneralCategory_Code && o.GeneralCategory_Id != gc.GeneralCategory_Id
                             select o).ToList();

                if (query.Count == 0)
                {
                    GeneralCategory gcQuery = (from o in dc.GeneralCategories
                                               where o.GeneralCategory_Id == gc.GeneralCategory_Id
                                               select o).Single();
                    gcQuery.GeneralCategory_Code = gc.GeneralCategory_Code;
                    gcQuery.GeneralCategory_Name = gc.GeneralCategory_Name;
                    dc.SubmitChanges();

                    result = 1;
                }
                else if (query.Count > 0)
                {
                    result = -2;
                }
            }

        }
        catch (Exception ex)
        {
            result = -1;
        }

        return result;
    }

    private bool Delete(long GeneralCategory_Id)
    {
        bool result = true;
        try
        {
            using (Up1VNDataContext dc = new Up1VNDataContext())
            {
                var query = from o in dc.GeneralCategories where o.GeneralCategory_Id == GeneralCategory_Id select o;
                dc.GeneralCategories.DeleteAllOnSubmit(query);
                dc.SubmitChanges();
            }
            result = true;
        }
        catch (Exception ex)
        {
            result = false;
        }
        return result;
    }

    private void Search(long GeneralCategory_Type, string GeneralCategory_Name)
    {
        try
        {
            gridCategoryJob.DataSource = null;

            Up1VNDataContext dc = new Up1VNDataContext();
            {
                var query = from o in dc.GeneralCategories 
                            where o.GeneralCategory_Name.Contains(GeneralCategory_Name) &&  o.GeneralCategory_Type == GeneralCategory_Type
                            orderby o.GeneralCategory_Name select o;
                if (query.Count() >= 0)
                {
                    Session["CategoryJob"] = query;

                    gridCategoryJob.DataSource = query;
                    gridCategoryJob.DataBind();
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private bool CheckValidValue(string Code, string Name)
    {
        bool result = true;
        if (string.IsNullOrEmpty(Code))
        {
            result = false;
        }
        if (string.IsNullOrEmpty(Name))
        {
            result = false;
        }

        return result;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        string strCode = txtCategoryJobCode.Text.Trim();
        string strName = txtCategoryJobName.Text.Trim();
        if (CheckValidValue(strCode, strName))
        {
            GeneralCategory gc = new GeneralCategory();
            gc.GeneralCategory_Id = 0;
            gc.GeneralCategory_Code = strCode;
            gc.GeneralCategory_Name = strName;
            gc.GeneralCategory_Type = GeneralCategoryType.CategoryJob;
            int intResult = Insert(gc);
            if (intResult == -1)
            {
                ShowMessage("Lưu không thành công");
            }
            else if (intResult == -2)
            {
                ShowMessage("Trùng mã tỉnh thành");
            }
            else
            {
                LoadAll(GeneralCategoryType.CategoryJob);
            }
        }
    }

    private void ShowMessage(string strMessage)
    {
        string strScript = "alert('" + strMessage + "')";
        ajaxmanagerCategoryJob.ResponseScripts.Add(strScript);
    }

    protected void gridCategoryJob_DeleteCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
        Telerik.Web.UI.GridEditableItem item = (GridEditableItem)e.Item;
        long GeneralCategory_Id = Convert.ToInt64(item.GetDataKeyValue("GeneralCategory_Id").ToString());
        bool result = Delete(GeneralCategory_Id);
        if (result)
        {
            ShowMessage("Xóa dữ liệu thành công");
            LoadAll(GeneralCategoryType.CategoryJob);
        }
        else
        {
            ShowMessage("Xóa dữ liệu không thành công");
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Search(GeneralCategoryType.CategoryJob, txtSearchCategoryJobName.Text.Trim());
    }
    protected void gridCategoryJob_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        gridCategoryJob.DataSource = Session["CategoryJob"];
    }
    protected void gridCategoryJob_UpdateCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
        GridEditManager editMan = (e.Item as GridEditableItem).EditManager;
        string strCode = ((GridTextColumnEditor)editMan.GetColumnEditor("GeneralCategory_Code")).Text;
        string strName = ((GridTextColumnEditor)editMan.GetColumnEditor("GeneralCategory_Name")).Text;

        if (CheckValidValue(strCode, strName))
        {
            Telerik.Web.UI.GridEditableItem item = (GridEditableItem)e.Item;
            long GeneralCategory_Id = Convert.ToInt64(item.GetDataKeyValue("GeneralCategory_Id").ToString());

            GeneralCategory gc = new GeneralCategory();
            gc.GeneralCategory_Id = GeneralCategory_Id;
            gc.GeneralCategory_Code = strCode;
            gc.GeneralCategory_Name = strName;
            gc.GeneralCategory_Type = GeneralCategoryType.CategoryJob;
            int intResult = Update(gc);
            if (intResult == -1)
            {
                ShowMessage("Cập nhật không thành công");
            }
            else if (intResult == -2)
            {
                ShowMessage("Trùng mã tỉnh thành");
            }
            else
            {
                LoadAll(GeneralCategoryType.CategoryJob);
                ShowMessage("Cập nhật thành công");
            }
        }
    }   
}